package com.imcoke.abchat.aspect;

import com.imcoke.abchat.common.AppProperties;
import com.imcoke.abchat.domain.Log;
import com.imcoke.abchat.domain.json.JsonUrl;
import com.imcoke.abchat.domain.ResultBody;
import com.imcoke.abchat.web.mapper.LogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @Author ccoke
 * @Description
 * @Date: 13:07 2017-11-30
 */
@Aspect
@Component
@Order(2)
public class LogAspect {
    @Autowired
    private LogMapper mLogMapper;

    @Pointcut("execution(* com.imcoke.abchat.web.controller.*.*(..))")
    public void logPointcut(){}

    @Around("logPointcut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Log log = null;
        Object vrt = pjp.proceed(pjp.getArgs());
        Signature signature = pjp.getSignature();
        String methodName = signature.getName();
        switch (methodName){
            case "postLogin": {
                ResponseEntity<ResultBody> obj = (ResponseEntity<ResultBody>) vrt;
                if (obj.getBody().getCode() == "200") {
                    log = new Log(null, getUserName((Model) pjp.getArgs()[1]), "成功申请了token", new Date(), getIp());
                    mLogMapper.insertLog(log);
                }
                break;
            }
            case "putReset": {
                ResponseEntity<ResultBody> obj = (ResponseEntity<ResultBody>) vrt;
                if (obj.getBody().getCode() == "200"){
                    log = new Log(null,getUserName((Model) pjp.getArgs()[1]),"通过邮箱重设了密码",new Date(),getIp());
                    mLogMapper.insertLog(log);
                }
                break;
            }
            case "putPassword":{
                ResponseEntity<ResultBody> obj = (ResponseEntity<ResultBody>) vrt;
                if (obj.getBody().getCode() == "200"){
                    log = new Log(null,getUserName(),"重新设置了密码",new Date(),getIp());
                    mLogMapper.insertLog(log);
                }
                break;
            }
            case "getEmail":{
                String obj = (String) vrt;
                if ("html/success".equals(vrt)){
                    //从token中提取邮箱
                    log = new Log(null,getUserName((Model) pjp.getArgs()[1]),"重新设置了邮箱地址",new Date(),getIp());
                    mLogMapper.insertLog(log);
                }
                break;
            }

            case "putBasic":{
                ResponseEntity<ResultBody> obj = (ResponseEntity<ResultBody>) vrt;
                if (obj.getBody().getCode() == "200"){
                    log = new Log(null,getUserName(),"重新设置了基本信息",new Date(),getIp());
                    mLogMapper.insertLog(log);
                }
                break;
            }

            case "uploadImage":{
                ResponseEntity<ResultBody> obj = (ResponseEntity<ResultBody>) vrt;
                if (obj.getBody().getCode() == "200"){
                    JsonUrl jsonUrl = (JsonUrl) obj.getBody().getPayload();
                    log = new Log(null,getUserName(),"上传了文件:" + jsonUrl.getUrl(),new Date(),getIp());
                    mLogMapper.insertLog(log);
                }
                break;
            }
        }
        return vrt;
    }

    private String getUserName(){
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    private String getIp(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //请求的IP
        return request.getRemoteAddr();
    }

    private String getUserName(Model model){
        Map<String, Object> map = model.asMap();
        return (String) map.get(AppProperties.USERNAME);
    }

}
